/*
* ============================================================================
* 功能描述 ：杂项功能
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/
#include "main.h"
unsigned char specialSumCheck(unsigned char *str, int len)
{
    int totalSum = 0;
	int i;

    for (i = 0; i < len; i++)
    {
        totalSum += *str++;
        // No need to calculate the modulus in each iteration
    }

    // Calculate the modulus after the loop
    totalSum %= 256;

    return (unsigned char)totalSum & 0xff;
}


/***********************************************************
函数名称：DelayXms
函数功能：毫秒级非精准延时
入口参数：x - 延时时间
出口参数：
备    注：
***********************************************************/
void DelayXms(unsigned char x)
{
	int i,j;
	for(i=x;i>0;i--)
		for(j=210;j>0;j--);
}

void Delayus(int x)
{
	int i;
	for(i=x;i>0;i--)
	{
	}
		
}
unsigned long hly_get_elapsed_ms(unsigned long old_timestamp)
{
  	unsigned long cur_ticks = get_current_ms();

   //计算两者的时间戳差值
    if( cur_ticks < old_timestamp ) //脉冲数溢出情况
    {
        return( cur_ticks + ( 0xFFFFFFFF - old_timestamp ) );
    }
    
	return( cur_ticks - old_timestamp );
}



char voltage_percentage(unsigned short int ch)
{
	char i;
	char val = 0;
	double adc_max = 3116, adc_min = 2524, jx = ch;
	double rangeDiff = adc_max - adc_min;
	
	if(jx >= adc_max)
	{
		val = 100;
		return val;
	}
	if(jx > adc_min)
	{
		val = ((jx - adc_min)/rangeDiff)*100;
		return val;
	}
	if(jx <= adc_min)
	{
		val = 0;
		return val;
	}
	

}

void hly_Obtain_voltage(void)
{
	unsigned  int adc;
	char i, val, ch[3];
	RB3 = 1;
	DelayXms(2);
	adc = Read_ADC(14, 1);
	
	ch[0] = SHORT_GET_HIGH_BYTE(adc);
	ch[1] = SHORT_GET_LOW_BYTE(adc);

	Uart_Tx_buf(ch, 2);
	
	val = voltage_percentage(adc);
	RB3 = 0;
	app_data.power_supply = val;
}

